package org.hypnus.e.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.hypnus.e.exception.EException;
import org.w3c.dom.Document;

public abstract class XmlUtil {

	public static DocumentBuilderFactory getDocBuilderFactory() {
		return DocumentBuilderFactory.newInstance();
	}

	public static Document loadXmlAsDoc(InputStream is) {
		Document doc = null;
		try {
			DocumentBuilder builder = getDocBuilderFactory().newDocumentBuilder();
			doc = builder.parse(is);
		} catch (Exception e) {
			throw new EException("parser xml as document error!",e) ;
		}
		return doc;
	}
	
	public static Document loadXmlAsDoc(File file){
		InputStream in = null;
		try {
			in = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			throw new EException("can't not locate the file ! filename==>"+file.getName(),e) ;
		}
		return loadXmlAsDoc(in) ;
	} 
}
